package zhongbo.java.air.monitor.web.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.monitor.model.FlightActivityDaily;
import zhongbo.java.air.monitor.service.IFlightActivityDailyService;
import zhongbo.java.air.monitor.task.AircraftActivityTask;

import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 03:35:26
 */
@RestController
@RequestMapping("flightPath")
@Api(tags = "航迹数据接口")
public class FlightActivityDailyController {

    @Autowired
    private IFlightActivityDailyService flightActivityDailyService;

    @Autowired
    private AircraftActivityTask aircraftActivityTask;

    @ApiOperation("查询当天分组统计结果")
    @GetMapping("getDailyStats")
    public ResultVo<List<FlightActivityDaily>> getDailyStats(String date) {
        // 如果不传, 则默认查当天的统计结果
        if (StrUtil.isBlank(date)) {
            date = DateUtil.today();
        }
        List<FlightActivityDaily> dailyList = flightActivityDailyService.getDailyStats(date);
        return ResultVo.ok(dailyList);
    }

    @ApiOperation("手动触发定时任务")
    @GetMapping("/trigger")
    public ResultVo<String> triggerTask() {
        aircraftActivityTask.updateDailyActivity();
        return ResultVo.ok("触发定时任务");
    }




    /* 以下是复用基础crud */

    // todo 今日周二待做, 标准crud接口(包括分页)

    @ApiOperation("手动增加航迹数据信息")
    @PostMapping("/addFlightActivityDaily")
    public ResultVo<String> addAirFlightPath(@RequestBody FlightActivityDaily flightActivityDaily) {
        String add = flightActivityDailyService.addFlightActivityDaily(flightActivityDaily);
        if (StrUtil.isNotBlank(add)) {
            return ResultVo.ok(add);
        }
        return ResultVo.error("新增航迹记录失败");
    }

    @ApiOperation("修改航迹记录")
    @PutMapping("/updateFlightActivityDaily")
    public ResultVo<FlightActivityDaily> updateFlightActivityDaily(@RequestBody FlightActivityDaily flightActivityDaily) {
        FlightActivityDaily update = flightActivityDailyService.updateFlightActivityDaily(flightActivityDaily);
        if (update != null) {
            return ResultVo.ok(update);
        }
        return ResultVo.error("修改航迹记录失败");
    }

    @ApiOperation("查询一个航行轨迹")
    @GetMapping("/getFlightActivityDaily")
    public ResultVo<FlightActivityDaily> getFlightActivityDaily(@RequestParam(defaultValue = "1") String airId) {
        FlightActivityDaily flightActivityDaily = flightActivityDailyService.getFlightActivityDailyById(airId);
        return ResultVo.ok(flightActivityDaily);
    }

    @ApiOperation("删除一个航行轨迹")
    @DeleteMapping("/deleteFlightActivityDaily")
    public ResultVo<Integer> deleteFlightActivityDaily(@RequestParam(defaultValue = "1") String airId) {
        int delete = flightActivityDailyService.deleteFlightActivityDaily(airId);
        return ResultVo.ok(delete);
    }


    @ApiOperation("分页获取所有航行轨迹")
    @GetMapping("pageAllFlightActivityDaily")
    public ResultVo<PageVo<FlightActivityDaily>> page(@RequestParam(required = false) String icaoCode,
                                                @RequestParam(defaultValue = "1") Integer pageNumber,
                                                @RequestParam(defaultValue = "1") Integer pageSize) {
        PageVo<FlightActivityDaily> page = flightActivityDailyService.page(icaoCode,pageNumber,pageSize);
        return ResultVo.ok(page);
    }

//    @ApiOperation("分页获取所有航行轨迹(可以设置时间)")
//    @GetMapping("/page")
//    public ResultVo<PageVo<FlightActivityDaily>> pageFlightPathAll (@RequestParam(required = true) String icao,
//                                                              @RequestParam(defaultValue = "2025-04-02 14:42:40.039") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") Date startTime,
//                                                              @RequestParam(defaultValue = "2025-04-07 11:11:41.266") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") Date endTime,
//                                                              @RequestParam(defaultValue = "1") Integer pageNumber,
//                                                              @RequestParam(defaultValue = "1") Integer pageSize) {
//        PageVo<FlightActivityDaily> page = flightActivityDailyService.pageTimeFlightPathAll(icao, startTime, endTime, pageNumber, pageSize);
//        return ResultVo.ok(page);
//    }
}